YOSUtil = {};
//解决ie下ext.Window中下拉框在window最大化后还原布局混乱的问题。
//
//参数：boxId-需要自适应的控件id
YOSUtil.dapBox = function(boxId){
	Ext.getCmp(boxId).ownerCt.on('resize', function(){
		var width = Ext.getCmp(boxId).getWidth();
		Ext.getCmp(boxId).setWidth(width - 2);		
	});
};

/**
 * 获取某combobox的显示值
 * 参数：combobox组件,valueField,displayField
 */
YOSUtil.getComboDisplayValue = function(combobox,valueField,displayField){
	if(!combobox){
		return '';
	}
	if(!combobox.getStore()){
		return '';
	}
	var allRecs = combobox.getStore().getRange();
	for(var i = 0; i < allRecs.length; i++){
		if(allRecs[i].get(valueField) == combobox.getValue()){
			return allRecs[i].get(displayField);
		}
	}
	return '';
};

//解决ext表格多选在某些浏览器下无效的问题
YOSUtil.multiSelectGrid = function(gridpanel){
	if(gridpanel && gridpanel.getSelectionModel()){
		var selectionModel = gridpanel.getSelectionModel();
		selectionModel.singleSelect = false;
		selectionModel.handleMouseDown = Ext.emptyFn;
		
		gridpanel.addListener('cellclick', function(grid,rowIndex,columnIndex,e){
			if(columnIndex != 0){
				if(selectionModel.isSelected(rowIndex)){
					selectionModel.deselectRow(rowIndex);
				}else{
					selectionModel.selectRow(rowIndex,true);
				}
			}
		});
	}
};

//表格提示
YOSUtil.gridTipRenderer = function(data, metadata){
	if(!Ext.QuickTips.isEnabled()){
		Ext.QuickTips.init();
	}
	var title ="";  
    var tip =data;   
    metadata.attr = 'ext:qtitle="' + title + '"' + ' ext:qtip="' + tip + '"';    
    return data;
};

//数组是否包含元素，参数：任意值；返回值：不包含返回-1，包含则返回索引值
Array.prototype.indexOf = function(value){
	for(var index in this){
		if(this[index] == value){
			return index;
		}
	}
	return -1;
};

YOSUtil.isBlankString = function(s){
	if(typeof(s) != "undefined" && s != null && s != ''){
		return false;
	}
	return true;
};

YOSUtil.selectText = function(dom) {
    if (document.selection) {
        var range = document.body.createTextRange();
        range.moveToElementText(dom);
        range.select();
    } else if (window.getSelection) {
        var range = document.createRange();
        range.selectNode(dom);
        window.getSelection().addRange(range);
    }
}

//单例数组/队列，最新插入的数据永远保持在最前（已有数据则更新）
YOSUtil.SingleArray = function(){
	var arr = [];
	
	this.push = function(e){
		for(var i = 0; i < arr.length; i++){
			if(arr[i] == e){
				arr.splice(i, 1);
				break;
			}
		}
		arr.push(e);
	};
	
	this.pop = function(){
		return arr.pop();
	};
	
	this.get = function(index){
		return arr[index];
	};
	
	this.clear = function(){
		arr = [];
	};
	
	this.length = function(){
		return arr.length;
	};
	
	this.indexOf = function(e){
		return arr.indexOf(e);
	};
	
	this.del = function(e){
		var index = arr.indexOf(e);
		if(index > -1){
			arr.splice(index, 1);
		}
	};
};

//利用“#”在html页面间传递参数
YOSUtil.htmlParams = function(){
	var paramMaps = [];
	var href = document.location.href;
	var paramSplitTmp = href.split('#');
	if(paramSplitTmp.length < 2){
		return paramMaps;
	}
	var paramStrs = paramSplitTmp[1].split('&');
	for(var i = 0; i < paramStrs.length; i++){
		paramExp = paramStrs[i].split('=');
		if(paramExp.length > 1){
			paramMaps[paramExp[0]] = paramExp[1];
		}else{
			paramMaps[paramExp[0]] = true;
		}
	}
	return paramMaps;
};

//动态加载css文件
YOSUtil.loadCss = function(cssPath){
	if(!document.getElementById(cssPath)){
		var container = document.getElementsByTagName("head")[0];
	    var addCss = document.createElement("link");
	    addCss.rel = "stylesheet";
	    addCss.type = "text/css";
	    addCss.media = "screen";
	    addCss.id = cssPath;
	    addCss.href = cssPath;
	    container.appendChild(addCss);
	}
};

//动态加载js文件，同步加载，会阻塞后续代码
YOSUtil.loadJs = function(jsPath){
	if(!document.getElementById(jsPath)){
		Ext.Ajax.request({
			url: jsPath,
			method: 'post',
			sync: true,
			callback: function(options, success, response){
				var container = document.getElementsByTagName("head")[0];
				var addScript = document.createElement( "script" ); 
				addScript.language = "javascript"; 
				addScript.type = "text/javascript"; 
				addScript.id = jsPath; 
				addScript.defer = true; 
				addScript.text = response.responseText;
				container.appendChild(addScript);
			}
		});
	}
};